//
//  ZJStudyVideoTableViewCell.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/5/2.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJStudyVideoTableViewCell.h"
#import "ZJAdditions.h"
#import "CustomLabel.h"
#import <SDAutoLayout.h>
#import "UIImageView+WebCache.h"
@interface ZJStudyVideoTableViewCell ()
/** 图片 */
@property (nonatomic, strong) UIImageView *imageview;
/** cell类型图片 */
@property (nonatomic, strong) UIImageView *typeImageView;
/** 题目 */
@property (nonatomic, strong) UILabel *titleLabel;
/** 主讲人 */
@property (nonatomic, strong) UILabel *speaker;
/** 点击量 */
@property (nonatomic, strong) UILabel *clickNumber;
/** 分割线 */
@property (nonatomic, strong) UILabel *underline;
@end

@implementation ZJStudyVideoTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.imageview];
        [self.contentView addSubview:self.typeImageView];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.speaker];
        [self.contentView addSubview:self.clickNumber];
        [self.contentView addSubview:self.underline];
        
        self.underline.sd_layout
        .bottomSpaceToView(self.contentView, 0)
        .leftSpaceToView(self.contentView, 0)
        .rightSpaceToView(self.contentView, 0)
        .heightIs(7.5 * [UIScreen zj_screenHScale]);
        
        self.imageview.sd_layout
        .topSpaceToView(self.contentView, 12 * [UIScreen zj_screenHScale])
        .leftSpaceToView(self.contentView, 12 * [UIScreen zj_screenWScale])
        .bottomSpaceToView(self.underline, 12 * [UIScreen zj_screenHScale])
        .widthIs(140 * [UIScreen zj_screenWScale]);
        
        
        self.typeImageView.sd_layout
        .centerXEqualToView(self.imageview)
        .centerYEqualToView(self.imageview)
        .widthIs(30 * [UIScreen zj_screenHScale])
        .heightIs(30 * [UIScreen zj_screenHScale]);
        
        self.titleLabel.sd_layout
        .topEqualToView(self.imageview)
        .leftSpaceToView(self.imageview, 8.5 * [UIScreen zj_screenWScale])
        .widthIs([UIScreen zj_screenWith] - (24 +8.5 + 140) * [UIScreen zj_screenWScale])
        .heightIs(46 * [UIScreen zj_screenHScale]);
        
        self.clickNumber.sd_layout
        .bottomEqualToView(self.imageview)
        .rightEqualToView(self.titleLabel)
        .widthIs(90 * [UIScreen zj_screenWScale])
        .heightIs([NSString heightWithTextFontSize:[UIFont subTitleTextFontSize]]);
        
        self.speaker.sd_layout
        .bottomEqualToView(self.clickNumber)
        .leftEqualToView(self.titleLabel)
        .heightRatioToView(self.clickNumber, 1)
        .rightSpaceToView(self.clickNumber, 0);
        
        
        
        self.titleLabel.text = @"啊看见萨斯uasiuadfasdf阿斯蒂芬就阿斯蒂芬卡洛斯大使馆暗改色额发世界杯撒办法水泵房";
    }
    return self;
}


#pragma mark - Setter
- (void)setModel:(ZJStudyRecommendModel *)model{
    _model = model;
    if ([model.IMAGEPHOTO_THUMBURL containsString:@"http:"]) {
        [self.imageview sd_setImageWithURL:[NSURL URLWithString:model.IMAGEPHOTO_THUMBURL] placeholderImage:PlaceholderImage];
    }else{
        [self.imageview sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASEIMAGEURL,model.IMAGEPHOTO_THUMBURL]] placeholderImage:PlaceholderImage];
    }
    self.titleLabel.text = model.NAME;
//    [self.titleLabel setLabelSpace:5 * [UIScreen zj_screenHScale]];
    
    self.titleLabel.sd_layout
    .topEqualToView(self.imageview)
    .leftSpaceToView(self.imageview, 8.5 * [UIScreen zj_screenWScale])
    .widthIs([UIScreen zj_screenWith] - (24 +8.5 + 140) * [UIScreen zj_screenWScale])
    .heightIs([self.titleLabel getSpaceLabelHeightFromSpace:5 * [UIScreen zj_screenHScale]]);
    [self.titleLabel updateLayout];
    
    self.speaker.text = [NSString stringWithFormat:@"来源 : %@", model.TEACHERSNAME];
    self.clickNumber.text = [NSString stringWithFormat:@"点击量 : %@", model.CLICKRATE];
    
    if ([model.COURSETYPE isEqualToString:@"1"]) {//主课
        self.typeImageView.image = [UIImage imageNamed:@"learn_list_view"];
        
    }else if ([model.COURSETYPE isEqualToString:@"2"]){//微课
        self.typeImageView.image = [UIImage imageNamed:@"learn_list_listen"];
    
    }else if ([model.COURSETYPE isEqualToString:@"3"]){//直播
        self.typeImageView.image = [UIImage imageNamed:@"learn_list_watch"];
        self.clickNumber.text = model.CREATETIME;
    }else if ([model.COURSETYPE isEqualToString:@"4"]){//悦读会
        self.typeImageView.image = nil;
    }
    
    
    
    for (id view in self.contentView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
    }
    
    
    CGFloat space = 8 * [UIScreen zj_screenWScale];
    CGFloat left = 160.5 * [UIScreen zj_screenWScale];
    if (model.CATEGORY_NAMES.count > 0) {
        for (int i = 0; i < model.CATEGORY_NAMES.count; i ++) {
            CGFloat btWidth = [NSString sizeOfString:model.CATEGORY_NAMES[i][@"CATEGORY_NAME"] font: 12 * [UIScreen zj_textFontScale]  width:[UIScreen zj_screenWith]].width + 8 * [UIScreen zj_screenHScale];
            CGFloat btHeight = [NSString heightWithTextFont:[UIFont systemFontOfSize:12 * [UIScreen zj_textFontScale]]] + 4 * [UIScreen zj_screenHScale];
            
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, btWidth,btHeight )];
            button.backgroundColor = [UIColor themeColor];
            
            [button setTitle:model.CATEGORY_NAMES[i][@"CATEGORY_NAME"] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:12 * [UIScreen zj_textFontScale]];
            //不超过屏幕
            if ((left + btWidth) > [UIScreen zj_screenWith]) {
                return;
            }
            
            
            [self.contentView addSubview:button];
            button.sd_cornerRadius = @(btHeight  / 2.0);
            button.sd_layout
            .bottomSpaceToView(self.speaker, 3 * [UIScreen zj_screenHScale])
            .leftSpaceToView(self.contentView, left)
            .widthIs(btWidth)
            .heightIs(btHeight);
            
            left += btWidth + space;
        }
    }
    
    
}


- (void)setCollectModel:(ZJOptionalStudyModel *)collectModel{
    _collectModel = collectModel;
    if ([collectModel.IMAGEPHOTO_THUMBURL containsString:@"http:"]) {
        [self.imageview sd_setImageWithURL:[NSURL URLWithString:collectModel.IMAGEPHOTO_THUMBURL] placeholderImage:PlaceholderImage];
    }else{
        [self.imageview sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASEIMAGEURL,collectModel.IMAGEPHOTO_THUMBURL]] placeholderImage:PlaceholderImage];
    }
    
    self.titleLabel.text = collectModel.NAME;
    
    self.titleLabel.sd_layout
    .topEqualToView(self.imageview)
    .leftSpaceToView(self.imageview, 8.5 * [UIScreen zj_screenWScale])
    .widthIs([UIScreen zj_screenWith] - (24 +8.5 + 140) * [UIScreen zj_screenWScale])
    .heightIs([self.titleLabel getSpaceLabelHeightFromSpace:5 * [UIScreen zj_screenHScale]]);
    [self.titleLabel updateLayout];
    
    self.speaker.text = [NSString stringWithFormat:@"主讲人 : %@", collectModel.TEACHERSNAME];
    self.clickNumber.text = [NSString stringWithFormat:@"点击量 : %@", collectModel.CLICKRATE];
    
    if ([collectModel.COURSETYPE isEqualToString:@"1"]) {//主课
        self.typeImageView.image = [UIImage imageNamed:@"learn_list_view"];
        
    }else if ([collectModel.COURSETYPE isEqualToString:@"2"]){//微课
        self.typeImageView.image = [UIImage imageNamed:@"learn_list_listen"];
        
    }else if ([collectModel.COURSETYPE isEqualToString:@"3"]){//直播
        self.typeImageView.image = [UIImage imageNamed:@"learn_list_watch"];
        self.clickNumber.text = collectModel.CREATETIME;
    }else if ([collectModel.COURSETYPE isEqualToString:@"4"]){//悦读会
        self.typeImageView.image = nil;
    }
    
    
    
    for (id view in self.contentView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
    }
    
    
    CGFloat space = 8 * [UIScreen zj_screenWScale];
    CGFloat left = 160.5 * [UIScreen zj_screenWScale];
    if (collectModel.CATEGORY_NAMES.count > 0) {
        for (int i = 0; i < collectModel.CATEGORY_NAMES.count; i ++) {
            CGFloat btWidth = [NSString sizeOfString:collectModel.CATEGORY_NAMES[i][@"CATEGORY_NAME"] font: 12 * [UIScreen zj_textFontScale]  width:[UIScreen zj_screenWith]].width + 8 * [UIScreen zj_screenHScale];
            CGFloat btHeight = [NSString heightWithTextFont:[UIFont systemFontOfSize:12 * [UIScreen zj_textFontScale]]] + 4 * [UIScreen zj_screenHScale];
            
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, btWidth,btHeight )];
            button.backgroundColor = [UIColor themeColor];
            
            [button setTitle:collectModel.CATEGORY_NAMES[i][@"CATEGORY_NAME"] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:12 * [UIScreen zj_textFontScale]];
            //不超过屏幕
            if ((left + btWidth) > [UIScreen zj_screenWith]) {
                return;
            }
            
            
            [self.contentView addSubview:button];
            button.sd_cornerRadius = @(btHeight  / 2.0);
            button.sd_layout
            .bottomSpaceToView(self.speaker, 3 * [UIScreen zj_screenHScale])
            .leftSpaceToView(self.contentView, left)
            .widthIs(btWidth)
            .heightIs(btHeight);
            
            left += btWidth + space;
        }
    }
    

}

#pragma mark - Getters
- (UIImageView *)imageview{
    if (_imageview == nil){
        _imageview = [UIImageView new];
        _imageview.contentMode = UIViewContentModeScaleAspectFill;
        _imageview.clipsToBounds = YES;
    }
    return _imageview;
}
- (UIImageView *)typeImageView{
    if (_typeImageView == nil){
        _typeImageView = [UIImageView new];
        _typeImageView.contentMode = UIViewContentModeCenter;
        
    }
    return _typeImageView;
}

- (UILabel *)titleLabel{
    if (_titleLabel == nil){
        _titleLabel = [UILabel new];
        _titleLabel.textColor = [UIColor textBlackColor];
        _titleLabel.font = [UIFont titleTextFont];
        _titleLabel.numberOfLines = 2;
    }
    return _titleLabel;
}

- (UILabel *)speaker{
    if (_speaker == nil){
        _speaker = [UILabel new];
        _speaker.textColor = [UIColor textGrayColor];
        _speaker.font = [UIFont subTitleTextFont];
        
    }
    return _speaker;
}
- (UILabel *)clickNumber{
    if (_clickNumber == nil){
        _clickNumber = [UILabel new];
        _clickNumber.textColor = [UIColor textGrayColor];
        _clickNumber.font = [UIFont subTitleTextFont];
        _clickNumber.textAlignment = NSTextAlignmentRight;
    }
    return _clickNumber;
}

- (UILabel *)underline{
    if (_underline == nil){
        _underline = [UILabel new];
        _underline.backgroundColor = [UIColor viewBGGrayColor];
    }
    return _underline;
}

@end
